include ../utils/Makefile.incl

SOURCES = $(wildcard *.c)
OBJECTS = $(SOURCES:.c=.o)
ASMS    = $(SOURCES:.c=.s)
DEPENDS = $(SOURCES:.c=.d)
USERS   = $(wildcard ../user/*_user.o)
ELFS    = $(USERS:../user/%_user.o=%.elf)

all: $(ELFS)

$(ELFS): %.elf: ../user/%_user.o $(OBJECTS) context_switch.o
	$(LD) $(LDFLAGS) -Map $*.map -o $@ $^ -lio -lbwio -luser -lgcc
	
$(OBJECTS): %.o: %.s
	$(AS) $(ASFLAGS) -o $@ $<

$(ASMS): %.s: %.c
	$(XCC) -S $(CFLAGS) $<

$(DEPENDS): %.d: %.c
	$(XCC) -M $(CFLAGS) $< > $@.$$$$; sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; rm -f $@.$$$$

include $(DEPENDS)

.PHONY: clean
clean:
	mv context_switch.s context_switch.s.tmp
	rm -f *.elf *.s *.o *.map *.d
	mv context_switch.s.tmp context_switch.s
